package oceanpayment

import (
    "fmt"
	"errors"
	"github.com/astaxie/beego"
)
/*
<response><account>140105</account><terminal>14010559</terminal><signValue>F1525E61EEAF181A756CBBEFAFB7AD07AC892AFBBBCC13028E3D58A45E4274FD</signValue><methods>WX</methods><order_number>123456789</order_number><card_country></card_country><order_currency>CNY</order_currency><order_amount>0.01</order_amount><order_notes></order_notes><card_number></card_number><card_type></card_type><payment_country>ZZ</payment_country><payment_id>190302100725843010528</payment_id><payment_authType>0</payment_authType><payment_status>0</payment_status><payment_details>20068:Request IP is not in whitelist</payment_details><payment_solutions></payment_solutions><payment_risk></payment_risk></response>


<response><account>191208</account><terminal>19120801</terminal><signValue>444660641148699C819A0EC71A2CA6D9A32EAE64E86BE79A740A22E0DC910CF4</signValue><methods>WX</methods><order_number>123456789002</order_number><card_country></card_country><order_currency>CNY</order_currency><order_amount>0.01</order_amount><order_notes></order_notes><card_number></card_number><card_type></card_type><payment_country>GB</payment_country><payment_id>190306110304418120824</payment_id><payment_authType>0</payment_authType><payment_status>-1</payment_status><payment_details>Pending</payment_details><payment_solutions></payment_solutions><payment_risk></payment_risk><customer_id></customer_id><subscribe></subscribe><pay_bankCode></pay_bankCode><pay_barCode></pay_barCode><payment_bankInfo>{"timeStamp":"1551841384","signType":"MD5","package":"prepay_id=wx061103047930590cabd8d77f1507683853","nonceStr":"1551841384488","paySign":"4128989F467A9DAEE83F49276BD5E73D"}</payment_bankInfo></response>
*/


type PayReq struct {
	Account          string `form:"account"`
	Terminal         string `form:"terminal"`
	SignValue        string `form:"signValue"`
	NoticeUrl        string `form:"noticeUrl"`
	Methods          string `form:"methods"`
	OrderNumber      string `form:"order_number"`
	OrderCurrency    string `form:"order_currency"`
	OrderAmount      string `form:"order_amount"`
	CustomerId       string `form:"customer_id"`
	BillingFirstName string `form:"billing_firstName"`
	BillingLastName  string `form:"billing_lastName"`
	BillingEmail     string `form:"billing_email"`
	BillingCountry   string `form:"billing_country"`
	BillingIp        string `form:"billing_ip"`
	ProductName      string `form:"productName"`
	BackUrl          string `form:"backUrl"`
}

type PayRsp struct {
	Account          string `xml:"account"`
	Terminal         string `xml:"terminal"`
	SignValue        string `xml:"signValue"`
	Methods          string `xml:"methods"`
	OrderNumber      string `xml:"order_number"`
	CardCountry      string `xml:"card_country"`
	OrderCurrency    string `xml:"order_currency"`
	OrderAmount      string `xml:"order_amount"`
	OrderNotes       string `xml:"order_notes"`
	CardNumber       string `xml:"card_number"`
	CardType         string `xml:"card_type"`
	PaymentCountry   string `xml:"payment_country"`
	PaymentId        string `xml:"payment_id"`
	PaymentAuthType  string `xml:"payment_authType"`
	PaymentStatus    string `xml:"payment_status"`
	PaymentDetails   string `xml:"payment_details"`
	PaymentSolutions string `xml:"payment_solutions"`
	PaymentRisk      string `xml:"payment_risk"`
	PaymentBankInfo  string `xml:"payment_bankInfo,omitempty"`
}

func MicroAppPay(account,terminal,noticeUrl,orderId,currency,amount,openId,firstName,lastName,ipAddr,productName ,key string)(payInfo string, err error){
	req := new(PayReq)
	req.Account = account
	req.Terminal = terminal
	req.NoticeUrl = noticeUrl
	req.Methods = "WX"
	req.OrderNumber = orderId
	req.OrderCurrency = currency
	req.OrderAmount = amount
	req.CustomerId = openId
	req.BillingFirstName = firstName
	req.BillingLastName = lastName
	req.BillingEmail = openId + "@dhfapp.com"
	req.BillingCountry = "CN"
	req.BillingIp = ipAddr
	req.ProductName = productName
	req.BackUrl = "N/A"
	req.SignValue = makeOrderSign(req, key)
	reqURL := "https://secure.oceanpayment.com/gateway/directservice/pay"
	sdk := NewSDK(reqURL)
	rsp := new(PayRsp)
	_, err = sdk.Request(req, rsp)
	if err != nil {
		beego.Debug("OceanPayment MicroAppPay err:", err.Error())
		return
	}
	if rsp.PaymentStatus == "0" {
	   err = errors.New(rsp.PaymentDetails)
	}
	payInfo = rsp.PaymentBankInfo
	return
}

func makeOrderSign(payReq *PayReq, key string) string {
	str := payReq.Account
	str += payReq.Terminal
	str += payReq.OrderNumber
	str += payReq.OrderCurrency
	str += payReq.OrderAmount
	str += payReq.BillingFirstName
	str += payReq.BillingLastName
	str += payReq.BillingEmail
	str += key
	return fmt.Sprintf("%x", sHA256([]byte(str)))
}
